package com.graduation.apiservice.controller;

import com.graduation.common.enums.RoleEnum;
import com.graduation.common.enums.StatusEnum;
import com.graduation.common.utils.UserContext;
import com.graduation.pojo.dto.ChangePasswordDTO;
import com.graduation.pojo.dto.LoginUserDTO;
import com.graduation.pojo.po.User;
import com.graduation.pojo.vo.UserVO;
import com.graduation.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping("/login")
    public String login(@RequestBody LoginUserDTO body) {
        return userService.login(body);
    }

    @PostMapping("/register")
    public void register(@RequestBody User user) {
        user.setStatus(StatusEnum.ON);
        user.setRole(RoleEnum.USER);
        userService.saveUser(user);
    }

    @GetMapping("/currentUserInfo")
    public UserVO getCurrentUserInfo() {
        Long currentUserId = UserContext.getUser();
        return userService.getUserById(currentUserId);
    }

    @PostMapping("/changePassword")
    public void changePassword(@RequestBody ChangePasswordDTO changePasswordDTO) {
        Long currentUserId = UserContext.getUser();
        userService.changePassword(currentUserId, changePasswordDTO);
    }

    @PutMapping
    public void updateUser(@RequestBody User user) {
        userService.updateUser(user);
    }

}
